package com.example.forum.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.forum.domain.DiscussPost;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;

@Mapper
@Repository
public interface DiscussPostDao{
    //查询所有帖子
    @Select({"<script>",
                "select * from discuss_post ",
                "where status!=2 ",
                "<if test='userId!=0'>",
                    "and user_id=#{userId}",
                "</if>",
                "order by score desc,create_time desc",
                "limit #{start},#{size}",
            "</script>"})
    public List<DiscussPost> findAll(@Param("userId")String userId,@Param("start") int start,@Param("size") int size);

    //查询总记录数
    @Select({"<script>",
                "select count(id) from discuss_post ",
                "where status!=2 ",
                "<when test='userId!=0'>",
                    "and user_id=#{userId}",
                "</when>",
            "</script>"})
    public Integer findAllCount(@Param("userId")String userId);

    //增加一个帖子
    @Insert("insert into discuss_post values(null,#{userId},#{title},#{content},#{type},#{status},#{createTime},#{commentCount},#{score})")
    public void addDiscussPost(DiscussPost discussPost);

    //根据id查询一个帖子
    @Select("select * from discuss_post where id=#{id}")
    public DiscussPost findById(int id);

    //更改帖子的评论数量
    @Update("update discuss_post set comment_count=#{commentCount} where id=#{id}")
    public void updateCommentCount(int id,int commentCount);
}
